#!/bin/env bash
# change time zone
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-timezone Asia/Shanghai
rm /etc/yum.repos.d/CentOS-Base.repo
cp /vagrant/yum/*.* /etc/yum.repos.d/
mv /etc/yum.repos.d/CentOS7-Base-163.repo /etc/yum.repos.d/CentOS-Base.repo
# using socat to port forward in helm tiller
# install  kmod and ceph-common for rook
yum install -y wget curl conntrack-tools vim net-tools telnet tcpdump bind-utils socat ntp kmod ceph-common dos2unix


# enable ntp to sync time
echo 'sync time'
systemctl start ntpd
systemctl enable ntpd
echo 'disable selinux'
setenforce 0
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config

echo 'enable iptable kernel parameter'
cat >> /etc/sysctl.conf <<EOF
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl -p

# sudo bash -c 'cat <<EOF >  /etc/sysctl.d/k8s.conf
# net.bridge.bridge-nf-call-ip6tables = 1
# net.bridge.bridge-nf-call-iptables = 1
# net.ipv4.ip_forward=1
# EOF'
# sudo sysctl --system

echo 'set host name resolution'
cat >> /etc/hosts <<EOF
192.168.8.110 master
192.168.8.111 node1
192.168.8.112 node2
EOF

cat /etc/hosts

echo 'set nameserver'
echo "nameserver 223.5.5.5">>/etc/resolv.conf
echo "nameserver 223.6.6.6">>/etc/resolv.conf
cat /etc/resolv.conf

echo 'disable swap'
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab

#create group if not exists
egrep "^docker" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
  groupadd docker
fi

# if [ ! $(getent group docker) ];
# then 
#     sudo groupadd docker;
# else
#     echo "docker user group already exists"
# fi

usermod -aG docker vagrant
rm -rf ~/.docker/

# 添加软件源信息 Kubernetes
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
rpm --import https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key. gpg
rpm --import https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

# 添加软件源信息 docker
yum install -y yum-utils device-mapper-persistent-data lvm2
rpm --import https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# Step 3: 更新软件源信息
yum makecache fast

# install docker
yum -y install docker-ce
# Step 4: 开启Docker服务
systemctl enable docker && systemctl start docker

# 配置docker加速镜像
mkdir -p /etc/docker/
cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors" : [
      "https://ajvcw8qn.mirror.aliyuncs.com",
      "http://2595fda0.m.daocloud.io"
    ],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF

systemctl daemon-reload
systemctl restart docker

# install Kubernetes
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet


# sudo cat ./pull.sh  
# for i in \`kubeadm config images list\`; do   
#  imageName=${i#k8s.gcr.io/}  
#  sudo docker pull registry.aliyuncs.com/google_containers/$imageName  
#  sudo docker tag registry.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName  
#  sudo docker rmi registry.aliyuncs.com/google_containers/$imageName  
# done;

echo "Configure Kubectl to autocomplete"
source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should be installed first.
echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.

